package org.jetlinks.pro.notify.wechat.official;

import com.alibaba.fastjson.JSON;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.validator.ValidatorUtils;
import org.jetlinks.core.metadata.ConfigMetadata;
import org.jetlinks.core.metadata.DefaultConfigMetadata;
import org.jetlinks.core.metadata.types.StringType;
import org.jetlinks.pro.ConfigMetadataConstants;
import org.jetlinks.pro.notify.*;
import org.jetlinks.pro.notify.template.TemplateManager;
import org.jetlinks.pro.notify.template.TemplateProperties;
import org.jetlinks.pro.notify.template.TemplateProvider;
import org.jetlinks.pro.notify.wechat.WechatProvider;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Nonnull;

@Component
public class WechatOfficialNotifierProvider implements NotifierProvider, TemplateProvider {

    private final WebClient client;

    private final TemplateManager templateManager;


    public WechatOfficialNotifierProvider(TemplateManager templateManager, WebClient.Builder builder) {
        this.templateManager = templateManager;
        this.client = builder.build();
    }

    public static final DefaultConfigMetadata notifierConfig = new DefaultConfigMetadata("通知配置", "")
        .add("appId", "开发者ID(AppID)", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
        .add("secret", "开发者密码(AppSecret)", "", new StringType().expand(ConfigMetadataConstants.required.value(true)));

    public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("模版配置", "")
        .add("configId", "通知配置ID", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
        .add("touser", "接收者openid", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
        .add("wxTemplateId", "微信模板ID", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
        .add("url", "模板跳转链接", "不跳转可不填", new StringType())
        .add("miniProgramId", "跳转小程序appid", "不跳转可不填", new StringType())
        .add("miniProgramPath", "跳转小程序具体路径", "可不填", new StringType())
        .add("title", "模板标题", "自动获取", new StringType())
        .add("content", "模板内容", "自动获取", new StringType());

    @Nonnull
    @Override
    public NotifyType getType() {
        return DefaultNotifyType.weixin;
    }

    @Nonnull
    @Override
    public Provider getProvider() {
        return WechatProvider.officialMessage;
    }

    @Override
    public Mono<WechatOfficialMessageTemplate> createTemplate(TemplateProperties properties) {
        return Mono.fromSupplier(() -> ValidatorUtils.tryValidate(JSON.parseObject(properties.getTemplate(), WechatOfficialMessageTemplate.class)));
    }

    @Nonnull
    @Override
    public Mono<WeixinOfficialNotifier> createNotifier(@Nonnull NotifierProperties properties) {
        return Mono.defer(() -> {
            WechatOfficialProperties wechatCorpProperties = FastBeanCopier.copy(properties.getConfiguration(), new WechatOfficialProperties());
            return Mono.just(new WeixinOfficialNotifier(properties.getId(), client, ValidatorUtils.tryValidate(wechatCorpProperties), templateManager));
        });
    }

    @Override
    public ConfigMetadata getNotifierConfigMetadata() {
        return notifierConfig;
    }

    @Override
    public ConfigMetadata getTemplateConfigMetadata() {
        return templateConfig;
    }
}
